package com.et.machine.manager.api

import com.et.machine.store.DISABLED
import com.et.machine.store.Status

/**
 * Table of goods
 */
data class GoodsInfo(
    var uid: Int = 0,
    var box: Int = 0,
    var channel: Int = 0,
    var label: String = "",
    var priceCent: Int = 0,
    @Status var status: Int = DISABLED,
    var quantity: Int = 0,
    var barcode: String = "${box}0$channel",
    var name: String = "${box}0$channel",
    var image: String = "",
    var discount: Int = 100,
    var sid: Int = 0
) {
    companion object {
        private fun uid(box: Int, channel: Int): Int = box * 1000 + channel
        fun label(box: Int, channel: Int): String {
            if (box == 11) return String.format("%02d", channel)
            if (box < 5) return String.format("%d%02d", box, channel)
            if (box < 10) return String.format("")
            return "$box$channel"
        }

        fun create(box: Int, channel: Int): GoodsInfo {
            return GoodsInfo(
                uid = uid(box, channel),
                box = box,
                channel = channel,
                label = label(box, channel),
                priceCent = 0,
                discount = 100,
                status = DISABLED,
                quantity = 0
            )
        }

        fun create(
            box: Int,
            channel: Int,
            priceCent: Int,
            barcode: String,
            discount: Int,
            @Status status: Int,
            quantity: Int
        ): GoodsInfo {
            return GoodsInfo(
                uid = uid(box, channel),
                box = box,
                channel = channel,
                label = label(box, channel),
                priceCent = priceCent,
                barcode = barcode,
                discount = discount,
                status = status,
                quantity = quantity
            )
        }
    }

    override fun toString(): String {
        return "ChannelEntity(uid=$uid, box=$box, channel=$channel, label='$label', priceCent=$priceCent, status=$status, quantity=$quantity, barcode='$barcode', name='$name', image='$image', discount=$discount, sid=$sid)"
    }

}